"""
#!/usr/bin/env python
# -*- coding:utf-8 -*-
@Project : All_Designer
@File : ViewImage.py
@Author : RuanYaoHuang
@Time : 2023/3/13 11:08

"""
import os
import time

from PyQt5.QtCore import QThread, QDateTime


class SnapThread(QThread):
    def __init__(self, device, data, path, limitStyle, parent=None):
        super(SnapThread, self).__init__(parent)
        self.device = device
        self.data = data
        self.path = path
        self.limitStyle = limitStyle
        self.is_running = False
        node = self.device.nodemap.get_node(['PixelFormat'])
        self.pixelformat = node['PixelFormat'].value
        print(f'FormatPixel is :{self.pixelformat}')
    def run(self):
        self.is_running = True
        imageFormat = ".raw"
        currentTime = QDateTime.currentDateTime().toString("yyyy-MM-dd HH-mm-ss")  # 时间
        Folder = self.path + '/' + self.pixelformat + '_PlaneSnap' + currentTime
        if not os.path.exists(Folder):
            os.makedirs(Folder)
        limited = self.limitStyle
        limit = ['0', '10', '30', '60', '120', '180', '1', '10', '50',
                 '100', '1000', '2000', '3000']
        sleepTime = int(limit[limited])
        frames = int(limit[limited])
        count = 1
        while self.is_running:
            save_name = self.pixelformat + '_' + str(count) + imageFormat
            save_path = Folder + '/' + save_name
            # 时间限制
            if 0 < limited < 6:
                try:
                    self.data.tofile(save_path)
                    print(f'save image is {save_path}')
                    count += 1
                    time.sleep(sleepTime)
                except AttributeError as A:
                    print(f'Debug is {A}')
            # 数量限制
            elif 5 < limited < 13:
                for i in range(frames):
                    save_name = self.pixelformat + '_' + str(count) + imageFormat
                    save_path = Folder + '/' + save_name
                    self.data.tofile(save_path)
                    time.sleep(1)
                    print(f'save image is {save_path}')
                    count += 1
                self.is_running = False
            else:
                pass

        print(f'SnapThread is Stop, and is_runing value is : {self.is_running}')



    def stop(self):
        self.is_running = False
        self.wait()
        print(f'SnapThread is Stop, and is_runing value is : {self.is_running}')
